﻿
namespace Proxy
{
    class PrinterProxy : IPrintable
    {
        private string name;
        private Printer real;

        public PrinterProxy()
        {
        }

        public PrinterProxy(string name)
        {
            this.name = name;
        }


        #region IPrintable メンバ

        public void SetPrinterName(string name)
        {
            lock (this)
            {
                if (real != null)
                {
                    real.SetPrinterName(name);
                }

                this.name = name;
            }
        }

        public string GetPrinterName()
        {
            return name;
        }

        public void Print(string str)
        {
            Realize();
            real.Print(str);
        }

        #endregion


        private void Realize()
        {
            lock (this)
            {
                if (real == null)
                {
                    real = new Printer(name);
                }
            }
        }
    }
}
